#include "head.h"

/*
Usage:服务器主逻辑
*/
#define MAX_THREAD_NUM 2

int main(int argc, char *argv[])
{
    char ip[50] = {0};
    char port[10]= {0};
    read_config("./config.ini",port,ip);

    thread_poll poll;
    initThreadPoll(&poll,MAX_THREAD_NUM);

    int socket_fd;
    initTcpLink(&socket_fd,ip,port);

    int epoll_fd = epoll_create(1);
    addtoEpoll(epoll_fd,socket_fd);

    printf("server set, prepared to start session \n");

    while(true){
        struct epoll_event ready_set[10];
        int ready_num = epoll_wait(epoll_fd,ready_set,10,-1);
        for(int i=0; i<ready_num; i++){
            if(ready_set[i].data.fd == socket_fd){
                int client_fd = accept(socket_fd,NULL,NULL);
                ERROR_CHECK(client_fd,-1,"accept");
                pthread_mutex_lock(&poll.lock);
                enqueue(client_fd,&poll.sessions);
                pthread_cond_signal(&poll.cond);
                pthread_mutex_unlock(&poll.lock);
            }
        }
    }

    return 0;
}
